<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:organization:member:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_organization_member_form', query: {organization_id: dataForm.organization_id} })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>
        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:organization:member:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-input v-model="dataForm.keyword" :placeholder="i18n('common.please_input') + i18n('common.keyword')" clearable>
          </el-input>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70px">
          </el-table-column>

          <el-table-column prop="username" :label="i18n('organization.member.role_id')" width="80">
            <template v-slot="scope">
              <el-text v-if="scope.row.role">
                {{ scope.row.role.title }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column prop="username" :label="i18n('member.chinese_mobile')" width="120">
            <template v-slot="scope">
              <el-text v-if="scope.row.member">
                {{ scope.row.member.username }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('member.info')" min-width="300">
            <template v-slot="scope">
              <dl class="table_dl" v-if="scope.row.member">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.member.avatar">
                    <template v-slot:error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  <el-text>{{ i18n('member.chinese_nickname') }}：</el-text>
                  <el-link type="primary" :underline="false" @click="router.push({ name: 'module_member_list', query: { keyword: scope.row.member.chinese_nickname } })">
                    {{ scope.row.member.chinese_nickname }}
                  </el-link>
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ i18n('member.create_time') }}： {{ scope.row.member.create_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column prop="last_login_time" :label="i18n('member.last_login_time')" width="160">
            <template v-slot="scope">
              <el-text v-if="scope.row.member">
                {{ scope.row.member.last_login_time }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('organization.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="$t('organization.member.inquiry_mode')" width="120">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.inquiry_mode.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('organization.member.publish_mode')"
                :inactive-text="$t('organization.member.receive_mode')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'inquiry_mode')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.member.manager')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.manager"
                :active-value="1"
                :inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.yes')"
                :inactive-text="$t('common.no')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'manager')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.member.publisher')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.publisher"
                :active-value="1"
                :inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.yes')"
                :inactive-text="$t('common.no')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'publisher')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.member.supplier')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.supplier"
                :active-value="1"
                :inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.yes')"
                :inactive-text="$t('common.no')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'supplier')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="100">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:organization:member:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-member-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization/member')

  const dataForm = reactive([
    'organization_id',
    'keyword',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  onMounted(() => {
    if(query.organization_id) {
      dataForm.organization_id = parseInt(query.organization_id)
    }

    getDataList(model)
  })
</script>
